// axios 二次封装
import axios from 'axios'
// import nprogress from 'nprogress'
// import 'nprogress/nprogress.css'
axios.defaults.adapter = function(config) {
	return new Promise((resolve, reject) => {
		var settle = require('axios/lib/core/settle');
		var buildURL = require('axios/lib/helpers/buildURL');
		uni.request({
			method: config.method.toUpperCase(),
			url: buildURL(config.url, config.params, config.paramsSerializer),
			header: config.headers,
			data: config.data,
			dataType: config.dataType,
			responseType: config.responseType,
			sslVerify: config.sslVerify,
			complete: function complete(response) {
				// console.log("执行完成：", response)
				response = {
					data: response.data,
					status: response.statusCode,
					errMsg: response.errMsg,
					header: response.header,
					config: config
				};
				settle(resolve, reject, response);
			}
		})
	})
}
const requsets = axios.create({
    // baseURL:'http://localhost:3001',
    // baseURL:'http://172.17.147.23:3000',
    // baseURL:'http://106.55.165.19:3000',
    baseUrl: 'http://1.15.189.19:3000',
    timeout:5000
})

// requsets.defaults.headers.common["authorization"] =  localStorage.getItem('token')

// 请求拦截器
requsets.interceptors.request.use((config)=>{
    // nprogress.start()
    // requsets.defaults.headers.common["authorization"] =  localStorage.getItem('token')
    return config
})

// 响应拦截器
requsets.interceptors.response.use((res)=>{
    // nprogress.done()
    // console.log(res.data)
    // if(res.data.flag == false && (res.data.msg=="未登录，请先登录" || res.data.msg=="授权过期" || res.data.msg=='授权超时')){
    //     linkToLogin(res.data.msg)
    //     localStorage.removeItem('token')
    // }
    return res.data
},(error)=>{
    // nprogress.done()
    return Promise.reject(error)
})

export default requsets